package File;

import IO.Buffered;

import java.io.*;
import java.util.Enumeration;
import java.util.Vector;

/**
 * @author wuhan
 * @date 2019/10/12 15:30
 */
public class SplitFile {
    public static void main(String[] args) throws IOException {
//        splitFile();
        addUp();
    }
    public static void splitFile() throws IOException {
        FileInputStream fileInputStream=new FileInputStream("out.jpg");
        FileOutputStream fos=null;
        byte[] buf=new byte[1024*1024];
        int len=0;
        int count = 1;
        while ((len=fileInputStream.read(buf))!=-1){
            fos=new FileOutputStream((count++) +".part");
            fos.write(buf,0,len);
            fos.close();

        }
        fileInputStream.close();

    }
    public static void addUp() throws IOException {
        Vector<FileInputStream> v=new Vector<>();
        v.add(new FileInputStream("1.part"));
        v.add(new FileInputStream("2.part"));
        v.add(new FileInputStream("3.part"));
        v.add(new FileInputStream("4.part"));
        Enumeration<FileInputStream> elements = v.elements();
//        SequenceInputStream sequenceInputStream=new SequenceInputStream(elements);
        BufferedInputStream bufferedInputStream=new BufferedInputStream(new SequenceInputStream(elements));
        BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream("oout.jpg"));
//        FileOutputStream fos=new FileOutputStream("oouutt.jpg");

        int i=0;
        while ((i=bufferedInputStream.read())!=-1){
            bufferedOutputStream.write(i);
        }
        bufferedInputStream.close();
        bufferedOutputStream.close();

//        byte[] bytes=new byte[1024];
//        int len=0;
//        while ((len=sequenceInputStream.read(bytes))!=-1){
//            fos.write(bytes,0,len);
//        }
//        fos.close();
//        sequenceInputStream.close();


    }
}
